class Buff {

}

Buff.fire = {
    checkStart(role, home) {
        for (let soldier of home.troops) {
            if (soldier.isBuffOf(Buff.type.fire, role)) {
                soldier.dead();
            }
        }
    }
}
Buff.ice = {
    checkStart(role, home) {
        for (let soldier of home.troops) {
            if (soldier.isBuffOf(Buff.type.ice, role)) {
                soldier.dead();
            }
        }
    }
}
Buff.pest = {
    checkStart(role, home) {
        if (home.troops.some(soldier => soldier.isBuffOf(Buff.type.pest, role))) {
            let allPest = home.troops.filter(soldier => soldier.isBuff(Buff.type.pest));
            allPest.sort((a, b) => a.buffTime() - b.buffTime());
            let deadCount = Math.ceil(allPest.length / 2);
            if (deadCount) {
                for (let index = 0; index < allPest.length; index++) {
                    allPest[index].dead();
                }
                home.buff(1, Buff.type.pest, role);
            }
        }
    }
}
Buff.tempt = {
    checkStart(role, home) {
        let soldiers = home.troops.filter(soldier => soldier.isBuffOf(Buff.type.tempt, role));
        if (soldiers.length) {
            for (let soldier of soldiers) {
                soldier.dead();
            }
            role.ownHome.addReinforce(role, soldiers.length);
        }
    }
}
Buff.confusion = {
    checkStart(role, home) {
        if (home.troops.some(soldier => soldier.isBuffOf(Buff.type.confusion, role))) {
            let soldiers = home.troops.filter(soldier => soldier.isBuff(Buff.type.confusion));
            if (soldiers.length >= 3) {
                let count = Math.floor(soldiers.length / 3);
                home.kill(count);
                home.debuff(count, Buff.type.confusion);
            }
        }
    }
}
Buff.quiet = {
    checkStart(role, home) {
        let soldiers = home.troops.filter(soldier => soldier.isBuffOf(Buff.type.quiet, role));
        for (let i = 0; i < Math.ceil(soldiers.length / 2); i++) {
            soldiers[i].clearBuff();
        }
    }
}
Buff.shield = {
    kill() {
        return false;
    },
    checkStart(role, home) {
        let soldiers = home.troops.filter(soldier => soldier.isBuffOf(Buff.type.shield, role));
        for (let i = 0; i < Math.ceil(soldiers.length / 2); i++) {
            soldiers[i].clearBuff();
        }
    }
}

Buff.type = {
    fire: 'fire',
    ice: 'ice',
    pest: 'pest',
    tempt: 'tempt',
    confusion: 'confusion',
    quiet: 'quiet',
    shield: 'shield',
}

module.exports = Buff;